package com.hstcs.systmes;

import com.badlogic.ashley.core.Entity;
import com.badlogic.ashley.core.Family;
import com.badlogic.ashley.systems.IteratingSystem;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.hstcs.assets.Area;
import com.hstcs.assets.Common;
import com.hstcs.components.BoundsComponent;
import com.hstcs.components.MgComponent;
import com.hstcs.components.StateComponent;
import com.hstcs.components.TextureComponent;
import com.hstcs.components.TransformComponent;

public class MaogouSystem extends IteratingSystem {

	
	private Vector2 dirTmp = new Vector2();
	private Vector2 tmpMove = new Vector2();
	public static Vector2 tmpVector2 = new Vector2();
	
	
	public MaogouSystem() {
		super(Family.all(MgComponent.class).get());
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void processEntity(Entity entity, float deltaTime) {
		// TODO Auto-generated method stub
		StateComponent state = entity.getComponent(StateComponent.class);
		TransformComponent tc = entity.getComponent(TransformComponent.class);
		MgComponent mc = entity.getComponent(MgComponent.class);
		if(state.equal(MgComponent.STATE_WALK)|| state.equal(MgComponent.STATE_CATCH)){
		//	rotation(tc, mc, deltaTime);
			move(tc, mc, deltaTime);
			if(mc.dest.dst(tc.pos.x, tc.pos.y) <= 50){
				moveTurn(entity, state);
			}
		}
	}
	
	void move(TransformComponent tc, MgComponent mc, float deltaTime){
//        tmpMove.x =  MathUtils.cosDeg(tc.rotation -90) * deltaTime * tc.speed;
//        tmpMove.y = MathUtils.sinDeg(tc.rotation -90) * deltaTime * tc.speed;
        tmpMove.x =  MathUtils.cosDeg(mc.angleDest) * deltaTime * tc.speed;
        tmpMove.y = MathUtils.sinDeg(mc.angleDest) * deltaTime * tc.speed;
        tc.pos.x += tmpMove.x;
        tc.pos.y += tmpMove.y;
	}
	
	void rotation(TransformComponent tc, MgComponent mc, float deltaTime){

		Area.test(mc, tc);
		Vector2 dest = mc.dest;
        dirTmp.set(dest.x - tc.pos.x, dest.y - tc.pos.y);
        tc.rotation = (tc.rotation +360)%360;
        float angle = (dirTmp.angle() +90) % 360;
        if(Math.abs(angle - tc.rotation) % 360 > 5){
                if(((angle- tc.rotation) > 0 && Math.abs(angle - tc.rotation) <180) ||
                                ((angle- tc.rotation) < 0 &&Math.abs(angle - tc.rotation) >180))
                {
                        tc.rotation += Common.TURN_V * deltaTime;
                }else{
                        tc.rotation -= Common.TURN_V * deltaTime;
                }
        }
	}
	
	void moveTurn(Entity entity, StateComponent state) {
		TransformComponent tc = entity.getComponent(TransformComponent.class);
		MgComponent mc = entity.getComponent(MgComponent.class);
		if (state.equal(MgComponent.STATE_WALK)) {
			mc.srcPos.set(mc.dest);
			Area.setMgDest(mc.srcPos, mc.dest);
			tmpVector2.set(mc.dest.x - mc.srcPos.x, mc.dest.y - mc.srcPos.y);
			mc.angleDest = tmpVector2.angle() ;
			setFlip(entity);

		} else if (state.equal(MgComponent.STATE_CATCH)) {
			mc.srcPos.set(mc.dest);
			Area.setMgDest(mc.dest, mc.mg_type);
			tmpVector2.set(mc.dest.x - mc.srcPos.x, mc.dest.y - mc.srcPos.y);
			mc.angleDest = tmpVector2.angle() ;
		}
	}
	
	public boolean getMg(float x, float y, int pointer){
		for(Entity entity:getEntities()){
			BoundsComponent bc = entity.getComponent(BoundsComponent.class);
			if(true ==bc.bounds.contains(x, y)){
				MgComponent mc = entity.getComponent(MgComponent.class);
				StateComponent sc = entity.getComponent(StateComponent.class);
				sc.set(mc.STATE_DRAG);
				mc.pointer = pointer;
				return true;
			}
		}
		return false;
	}
	
	public void dragMg(float x, float y, int pointer){
		for(Entity entity:getEntities()){
			MgComponent mc = entity.getComponent(MgComponent.class);
			if(mc.pointer == pointer){
				TransformComponent tc = entity.getComponent(TransformComponent.class);
				tc.pos.x = x;
				tc.pos.y = y;
				return ;
			}
		}
	}
	
	public void touchupMg(float x, float y, int pointer){
		for(Entity entity:getEntities()){
			MgComponent mc = entity.getComponent(MgComponent.class);
			if(mc.pointer == pointer){
				mc.pointer = -1;
				TransformComponent tc = entity.getComponent(TransformComponent.class);
				StateComponent sc = entity.getComponent(StateComponent.class);
				int groundType = Area.isXGround(tc.pos.x, tc.pos.y);
				if(groundType == Area._CO_GROUND){
					sc.set(mc.STATE_WALK);
					return ;
				}else if( groundType == mc.mg_type){
					sc.set(MgComponent.STATE_CATCH);
					moveTurn(entity, sc);
				}else {
					//TODO
					sc.set(mc.STATE_WALK);
				}
				return ;
			}
		}
	}
	public void setFlip(Entity entity){
		System.out.println("3333333333----------------");
		MgComponent mc = entity.getComponent(MgComponent.class);
		TextureComponent tc = entity.getComponent(TextureComponent.class);
		if((mc.dest.x > mc.srcPos.x&& mc.mg_type==Common.TYPE_GOU)||
				(mc.dest.x < mc.srcPos.x&& mc.mg_type== Common.TYPE_MAO)){
			tc.flip = true;
		}else{
			tc.flip = false;
		}	

	}

}
